package copytranslation.hash;

import copytranslation.read.ReadExcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

/**
 *
 * @author srkrishnan
 */
public class Hash {

    static private HashMap translationmap = new HashMap();

    public static HashMap getTranslationmap() {
        return translationmap;
    }

    public static void addtoHeap(File file, ReadExcel readexcel) {
        InputStream input = null;
        POIFSFileSystem fs;
        HSSFWorkbook wb;
        HSSFSheet sheet;
        try {
            input = new FileInputStream(file);
            fs = new POIFSFileSystem(input);
            wb = new HSSFWorkbook(fs);
            sheet = wb.getSheetAt(0);

            
            
            String hashKey = "", hashValue = "";
            int Keyindex = readexcel.getKeyColumnindex();
            int valueindex = readexcel.getValueColumnindex();

            // Iterate over each row in the sheet
            Iterator rows = sheet.rowIterator();
            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();
                
                //get KEY and VALUE from row and put it in HashMap
                if (row.getCell(Keyindex) != null && row.getCell(Keyindex).getCellType()==Cell.CELL_TYPE_STRING) {
                    HSSFCell cell=row.getCell(Keyindex);
                    
                    hashKey = row.getCell(Keyindex).getStringCellValue();
                }
                if (row.getCell(valueindex) != null && row.getCell(valueindex).getCellType()==Cell.CELL_TYPE_STRING) {
                    hashValue = ""+row.getCell(valueindex).getStringCellValue();
                }
                //System.out.println("Key :"+hashKey+" Value :"+hashValue);
                translationmap.put(hashKey, hashValue);

            }


        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                input.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
